package com.heima.behavior.service.impl;

import com.heima.behavior.config.GetUser;
import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 文章业务类
 */
@Service("ApArticleBehaviorService")
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private GetUser getUser;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 加载文章详情 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        ApUser apUser = getUser.getApUser();
        HashMap<String, Boolean> hashMap = new HashMap<>();
        // 用户还未登录
        if (apUser == null) {
            hashMap.put("isfollow", false);
            hashMap.put("islike", false);
            hashMap.put("isunlike", false);
            hashMap.put("iscollection", false);
        } else {
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
            // 获取文章Id
            Long articleId = dto.getArticleId();

            //根据文章Id查询各个库中的 点赞,收藏,不喜欢数据
            //点赞
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("articleId").is(articleId).and("entryId").is(apBehaviorEntry.getId())), ApLikesBehavior.class);
            hashMap.put("islike", likesBehavior != null);

            //不喜欢
            ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("articleId").is(articleId).and("entryId").is(apBehaviorEntry.getId())), ApUnlikesBehavior.class);
            hashMap.put("isunlike", unlikesBehavior != null);

            //收藏
            ApCollection collection = mongoTemplate.findOne(Query.query(Criteria.where("articleId").is(articleId).and("entryId").is(apBehaviorEntry.getId())), ApCollection.class);
            hashMap.put("iscollection", collection != null);

            //从Redis中查询关注信息
            ZSetOperations zSetOperations = redisTemplate.opsForZSet();
            Double score = zSetOperations.score(UserRelationConstants.FOLLOW_LIST + apUser.getId(), dto.getAuthorId());
            hashMap.put("isfollow", score != null);

        }
        return ResponseResult.okResult(hashMap);
    }
}
